home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / fl_vertex.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-07  |  5.6 KB  |  228 lines

  1. //
  2. // "$Id: fl_vertex.cxx,v 1.5 1999/01/07 19:17:44 mike Exp $"
  3. //
  4. // Portable drawing routines for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. // Portable drawing code for drawing arbitrary shapes with
  27. // simple 2D transformations.  See also fl_arc.C
  28.  
  29. #include <FL/fl_draw.H>
  30. #include <FL/x.H>
  31. #include <FL/math.h>
  32. #include <stdlib.h>
  33.  
  34. struct matrix {double a, b, c, d, x, y;};
  35.  
  36. static matrix m = {1, 0, 0, 1, 0, 0};
  37.  
  38. static matrix stack[10];
  39. static int sptr = 0;
  40.  
  41. void fl_push_matrix() {stack[sptr++] = m;}
  42.  
  43. void fl_pop_matrix() {m = stack[--sptr];}
  44.  
  45. void fl_mult_matrix(double a, double b, double c, double d, double x, double y) {
  46.   matrix o;
  47.   o.a = a*m.a + b*m.c;
  48.   o.b = a*m.b + b*m.d;
  49.   o.c = c*m.a + d*m.c;
  50.   o.d = c*m.b + d*m.d;
  51.   o.x = x*m.a + y*m.c + m.x;
  52.   o.y = x*m.b + y*m.d + m.y;
  53.   m = o;
  54. }
  55.  
  56. void fl_scale(double x,double y) {fl_mult_matrix(x,0,0,y,0,0);}
  57.  
  58. void fl_scale(double x) {fl_mult_matrix(x,0,0,x,0,0);}
  59.  
  60. void fl_translate(double x,double y) {fl_mult_matrix(1,0,0,1,x,y);}
  61.  
  62. void fl_rotate(double d) {
  63.   if (d) {
  64.     double s, c;
  65.     if (d == 0) {s = 0; c = 1;}
  66.     else if (d == 90) {s = 1; c = 0;}
  67.     else if (d == 180) {s = 0; c = -1;}
  68.     else if (d == 270 || d == -90) {s = -1; c = 0;}
  69.     else {s = sin(d*M_PI/180); c = cos(d*M_PI/180);}
  70.     fl_mult_matrix(c,-s,s,c,0,0);
  71.   }
  72. }
  73.  
  74. static XPoint *p = (XPoint *)0;
  75. // typedef what the x,y fields in a point are:
  76. #ifdef WIN32
  77. typedef int COORD_T;
  78. #else
  79. typedef short COORD_T;
  80. #endif
  81.  
  82. static int p_size;
  83. static int n;
  84. static int what;
  85. enum {LINE, LOOP, POLYGON, POINT_};
  86.  
  87. void fl_begin_points() {n = 0; what = POINT_;}
  88.  
  89. void fl_begin_line() {n = 0; what = LINE;}
  90.  
  91. void fl_begin_loop() {n = 0; what = LOOP;}
  92.  
  93. void fl_begin_polygon() {n = 0; what = POLYGON;}
  94.  
  95. double fl_transform_x(double x, double y) {return x*m.a + y*m.c + m.x;}
  96.  
  97. double fl_transform_y(double x, double y) {return x*m.b + y*m.d + m.y;}
  98.  
  99. double fl_transform_dx(double x, double y) {return x*m.a + y*m.c;}
  100.  
  101. double fl_transform_dy(double x, double y) {return x*m.b + y*m.d;}
  102.  
  103. static void fl_transformed_vertex(COORD_T x, COORD_T y) {
  104.   if (!n || x != p[n-1].x || y != p[n-1].y) {
  105.     if (n >= p_size) {
  106.       p_size = p ? 2*p_size : 16;
  107.       p = (XPoint *)realloc((void*)p, p_size*sizeof(*p));
  108.     }
  109.     p[n].x = x;
  110.     p[n].y = y;
  111.     n++;
  112.   }
  113. }
  114.  
  115. void fl_transformed_vertex(double xf, double yf) {
  116.   fl_transformed_vertex(COORD_T(xf+.5), COORD_T(yf+.5));
  117. }
  118.  
  119. void fl_vertex(double x,double y) {
  120.   fl_transformed_vertex(x*m.a + y*m.c + m.x, x*m.b + y*m.d + m.y);
  121. }
  122.  
  123. void fl_end_points() {
  124. #ifdef WIN32
  125.   for (int i=0; i<n; i++) SetPixel(fl_gc, p[i].x, p[i].y, fl_RGB());
  126. #else
  127.   if (n>1) XDrawPoints(fl_display, fl_window, fl_gc, p, n, 0);
  128. #endif
  129. }
  130.  
  131. void fl_end_line() {
  132. #ifdef WIN32
  133.   if (n>1) Polyline(fl_gc, p, n);
  134. #else
  135.   if (n>1) XDrawLines(fl_display, fl_window, fl_gc, p, n, 0);
  136. #endif
  137. }
  138.  
  139. static void fixloop() {  // remove equal points from closed path
  140.   while (n>2 && p[n-1].x == p[0].x && p[n-1].y == p[0].y) n--;
  141. }
  142.  
  143. void fl_end_loop() {
  144.   fixloop();
  145.   if (n>2) fl_transformed_vertex((COORD_T)p[0].x, (COORD_T)p[0].y);
  146.   fl_end_line();
  147. }
  148.  
  149. void fl_end_polygon() {
  150.   fixloop();
  151. #ifdef WIN32
  152.   if (n>2) {
  153.     SelectObject(fl_gc, fl_brush());
  154.     Polygon(fl_gc, p, n);
  155.   }
  156. #else
  157.   if (n>2) XFillPolygon(fl_display, fl_window, fl_gc, p, n, Convex, 0);
  158. #endif
  159. }
  160.  
  161. static int gap;
  162. #ifdef WIN32
  163. static int counts[20];
  164. static int numcount;
  165. #endif
  166.  
  167. void fl_begin_complex_polygon() {
  168.   fl_begin_polygon();
  169.   gap = 0;
  170. #ifdef WIN32
  171.   numcount = 0;
  172. #endif
  173. }
  174.  
  175. void fl_gap() {
  176.   while (n>gap+2 && p[n-1].x == p[gap].x && p[n-1].y == p[gap].y) n--;
  177.   if (n > gap+2) {
  178.     fl_transformed_vertex((COORD_T)p[gap].x, (COORD_T)p[gap].y);
  179. #ifdef WIN32
  180.     counts[numcount++] = n-gap;
  181. #endif
  182.     gap = n;
  183.   } else {
  184.     n = gap;
  185.   }
  186. }
  187.  
  188. void fl_end_complex_polygon() {
  189.   fl_gap();
  190. #ifdef WIN32
  191.   if (n>2) {
  192.     SelectObject(fl_gc, fl_brush());
  193.     PolyPolygon(fl_gc, p, counts, numcount);
  194.   }
  195. #else
  196.   if (n>2) XFillPolygon(fl_display, fl_window, fl_gc, p, n, 0, 0);
  197. #endif
  198. }
  199.  
  200. // shortcut the closed circles so they use XDrawArc:
  201. // warning: these do not draw rotated ellipses correctly!
  202. // See fl_arc.c for portable version.
  203.  
  204. void fl_circle(double x, double y,double r) {
  205.   double xt = fl_transform_x(x,y);
  206.   double yt = fl_transform_y(x,y);
  207.   double rx = r * (m.c ? sqrt(m.a*m.a+m.c*m.c) : fabs(m.a));
  208.   double ry = r * (m.b ? sqrt(m.b*m.b+m.d*m.d) : fabs(m.d));
  209.   int llx = int(xt-rx+.5);
  210.   int w = int(xt+rx+.5)-llx;
  211.   int lly = int(yt-ry+.5);
  212.   int h = int(yt+ry+.5)-lly;
  213. #ifdef WIN32
  214.   if (what==POLYGON) {
  215.     SelectObject(fl_gc, fl_brush());
  216.     Pie(fl_gc, llx, lly, llx+w, lly+h, 0,0, 0,0); 
  217.   } else
  218.     Arc(fl_gc, llx, lly, llx+w, lly+h, 0,0, 0,0); 
  219. #else
  220.   (what == POLYGON ? XFillArc : XDrawArc)
  221.     (fl_display, fl_window, fl_gc, llx, lly, w, h, 0, 360*64);
  222. #endif
  223. }
  224.  
  225. //
  226. // End of "$Id: fl_vertex.cxx,v 1.5 1999/01/07 19:17:44 mike Exp $".
  227. //
  228.